package com.sanjiahantang.youjiaclient.module.common.holders;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyListView;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.cases.beans.CaseBean;
import com.sanjiahantang.youjiaclient.module.cases.views.impl.CaseActivity;
import com.sanjiahantang.youjiaclient.module.cases.views.impl.CaseDetailActivity;
import com.sanjiahantang.youjiaclient.module.goods.beans.GoodsBean;
import com.sanjiahantang.youjiaclient.module.match.views.impl.MatchListActivity;
import com.sanjiahantang.youjiaclient.module.store.beans.StoreBean;
import com.sanjiahantang.youjiaclient.module.store.views.impl.StoreActivity;
import com.sanjiahantang.youjiaclient.module.store.views.impl.StoreDetailActivity;
import com.sanjiahantang.youjiaclient.module.store.views.impl.StoreOnMapActivity;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_ID;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_LAT;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_LON;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_ROLEID;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TITLE;

/**
 * Created by Administrator on 2017/6/8.
 * 新品上市，热销商品，家用电器的ViewHolder
 */

public class ComprehensiveHolder extends MyBaseViewHolder implements View.OnClickListener {
    //三种标识
    private int type;
    private String currentType;
    private final int TYPE_CASE = 3;//案例
    private final int TYPE_NEARSTORE = 4;//附近体验店
    public static final int TYPE_MATCH_TITLE = 5;//搭配标题
    public static final int TYPE_MATCH = 6;//搭配

    private String name, enName;
    private Context context;
    private TextView tv_type, tv_more, tvEnglish;
    private MyListView lv_comprehen;
    private View rootView;

    private Object data;

    //这里只需要显示前几个就行
    private List<GoodsBean.Data> partDatas = new ArrayList<>();

    public ComprehensiveHolder(View itemView, final int type, final Context context, Object data, final String name, final String enName) {
        super(itemView);

        this.type = type;
        this.context = context;
        this.data = data;
        this.name = name;
        this.enName = enName;

        tv_type = (TextView) itemView.findViewById(R.id.tv_type);
        tv_more = (TextView) itemView.findViewById(R.id.tv_more);
        lv_comprehen = (MyListView) itemView.findViewById(R.id.lv_comoerhensive);
        tvEnglish = (TextView) itemView.findViewById(R.id.tv_comprehensive_en);
        rootView = itemView.findViewById(R.id.rootView);
    }

    @Override
    public void bindData() {
        tv_type.setText(name);
        tvEnglish.setText(enName);
        tv_more.setOnClickListener(this);
        switch (type) {
            case TYPE_CASE:
                rootView.setBackgroundColor(context.getResources().getColor(R.color.white));
                initCases((List<CaseBean.Item>) data);
                break;
            case TYPE_NEARSTORE:
                rootView.setBackgroundColor(context.getResources().getColor(R.color.bg_color));
                rootView.setPadding(0, 0, 0, Utils.dip2px(context, 20));
                initStores((List<StoreBean.Item>) data);
                break;
        }
    }

    private void initStores(List<StoreBean.Item> sdata) {
        QuickAdapter<StoreBean.Item> quickAdapter = new QuickAdapter<StoreBean.Item>(context, R.layout.item_store) {
            @Override
            protected void convert(BaseAdapterHelper helper, final StoreBean.Item item) {
                helper.setText(R.id.nearby_shop_name, item.getExperienceName());
                helper.setText(R.id.nearby_distance, "离你" + String.format("%.2f", item.getDistance()));
                helper.setText(R.id.nearby_address, (item.getExperienceAddress() == null ? "" : item.getExperienceAddress())
                        + (item.getExperienceAddressDetail() == null ? "" : item.getExperienceAddressDetail()));
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        context.startActivity(new Intent(context, StoreOnMapActivity.class)
//                                .putExtra(KEY_LAT, item.getLat())
//                                .putExtra(KEY_LON, item.getLon())
//                                .putExtra(KEY_TITLE, item.getExperienceName())
                        context.startActivity(new Intent(context, StoreDetailActivity.class)
                                .putExtra(KEY_ROLEID, item.getRoleId()));
                    }
                });

                helper.setOnClickListener(R.id.ivCall, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Utils.call(context, item.getLinkPersonPhone());
                    }
                });

                ImageView nearby_shop_img = helper.getView(R.id.nearby_shop_img);
                Picasso.with(context).load(FileUtils.rePath(item.getUrl())).resize(150,150).placeholder(R.drawable.i_rect_bg).into(nearby_shop_img);

                LinearLayout llDevice = helper.getView(R.id.llDevice);
                llDevice.removeAllViews();
                if (item.getIconList() != null) {
                    for (int i = 0, max = item.getIconList().size(); i < max; i++) {
                        ImageView imageView = new ImageView(context);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.dip2px(context, 15), Utils.dip2px(context, 15));
                        imageView.setLayoutParams(params);
                        StoreBean.Icon icon = item.getIconList().get(i);
                        if (icon != null)
                            Picasso.with(context).load(FileUtils.rePath(icon.getUrl())).into(imageView);
                        llDevice.addView(imageView);
                    }
                }

            }
        };
        quickAdapter.addAll(sdata);
        lv_comprehen.setAdapter(quickAdapter);
    }

    private void makePhoneCall(String phone) {

    }

    private void initCases(List<CaseBean.Item> cdata) {
        QuickAdapter<CaseBean.Item> quickAdapter = new QuickAdapter<CaseBean.Item>(context, R.layout.item_main_case) {
            @Override
            protected void convert(BaseAdapterHelper helper, final CaseBean.Item item) {
                helper.setText(R.id.txtCaseTitle, item.getCaseTitle());
                helper.setText(R.id.txtDesignerName, "设计师:  " + item.getUserName());
                Picasso.with(context).load(FileUtils.rePath(item.getHeadImg())).placeholder(R.drawable.i_head_default).into((ImageView) helper.getView(R.id.ivDesignerPhoto));
                Picasso.with(context).load(FileUtils.rePath(item.getCaseImg())).placeholder(R.drawable.i_match).into((ImageView) helper.getView(R.id.ivCaseMain));
                helper.setOnClickListener(R.id.ivCaseMain, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(new Intent(context, CaseDetailActivity.class)
                                .putExtra("param", item.getResidentialName())
                                .putExtra(KEY_ID, item.getId())
                        );
                    }
                });
            }
        };
        quickAdapter.addAll(cdata);
        lv_comprehen.setAdapter(quickAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (type) {
            case TYPE_CASE:
                context.startActivity(new Intent(context, CaseActivity.class));
                break;
            case TYPE_NEARSTORE:
                context.startActivity(new Intent(context, StoreActivity.class));
                break;
            case TYPE_MATCH_TITLE:
                context.startActivity(new Intent(context, MatchListActivity.class));
                break;
        }
    }
}
